package com.huobi.db.service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.huobi.db.mapper.PartitionMapper;

/**
 * 负责创建分表
 * @author jiangwei
 *
 */
@Service
@Scope("prototype")
public class PartitionService {

	private String currentDaily = StringUtils.EMPTY;

	public String getCurrentDaily() {
		return this.currentDaily;
	}

	// 看看是否创建了表
	public void partition(PartitionMapper partitionMapper) {

		final LocalDate now = LocalDate.now();
		final String daily = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

		if (StringUtils.equals(currentDaily, daily)) {
			return;
		}

		synchronized (this.currentDaily) {
			if (partitionMapper.existTable(daily) != 1) {
				partitionMapper.createTable(daily);
			}
			this.currentDaily = daily;
		}

	}

}
